function S = rs_syndromes(R,n,k,b)
%
% S = Rs_syndromes(R,n,k,b)
% 
% Calculates the syndrome values using the Horner’s method. 
% 
% This function uses the rs_init function in order the initialize the
% lookup tables that are saved as global variables. 
% 
% Tansu Demirbilek
% ademirbi@mc.com
% Mercury Computer Systems

m = R.m;
N = 2^m-1; 
f = N-n;            % Code shortening factor
K = k+f;    % (N,K,m) for the original (unshortened) RS code
p = R.prim_poly;

N_Tries = size(R,1);
gf_name = sprintf('GF_%d_%d_%d_%d_%d',N,K,m,b,p);
eval(sprintf('global %s',gf_name));

if(isempty( eval(gf_name)))
    rs_init(N,K,m,p,b);
end

% Create the local copies of the global variables
eval(sprintf('g = %s.g;',gf_name));
eval(sprintf('alpha_to = %s.alpha_to;',gf_name));
eval(sprintf('alpha_inv = %s.alpha_inv;',gf_name));
eval(sprintf('log_alpha = %s.log_alpha;',gf_name));

%% Calculate Syndromes
% Calculate R(alpha^i) using Horner's method to get syndrome values
% S = [S_0 S_1 ... S_(n-k-1)]
%fprintf('Calculating Syndrome Values\n');
S = gf(zeros(N_Tries,n-k), m,p);
R = [zeros(N_Tries,f) R];
for i=1:N_Tries
    S(i,1:end) = polyval(R(i,:),alpha_to( mymod(b:b+(n-k)-1,N) ) );
    % Reverse the Synd vector so it is
    % S = [S(n-k-1).S.. S_1 S_0]
    S(i,:) = S(i,end:-1:1);
    %fprintf('.');
    if (mod(i,64)==0)
        %fprintf('\n');
    end
end
%fprintf('Done\n\n');